package com.hlmc.controller;

import com.hlmc.entity.*;
import com.hlmc.service.FlService;
import com.hlmc.service.Query_360_Service;
import com.hlmc.service.YunService;
import com.hlmc.service.Zy2Service;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

@Controller
public class Query_360_Controller {

    @Resource
    private Query_360_Service query_360;

    @Resource
    private FlService fl_service;

    @Resource
    private YunService query_yun;

    @Resource
    private Zy2Service zy2Service;

    @ResponseBody
    @RequestMapping("/v_coverJson")
    public List<VideoCover> getJson(DS360_dt ds){
        List<VideoCover> cover=query_360.findCover(ds);
        return cover;
    }

    @RequestMapping("/v_coverJson2")
    public List<VideoCover> getJson2(DS360_dt ds){
        List<VideoCover> cover=query_360.findCover(ds);
        return cover;
    }

    @RequestMapping("/index")
    public String index(Model model){

        List<VideoCover> listCover=new ArrayList<VideoCover>();
        listCover=query_360.findHot1();
        List<Banner> banners=new ArrayList<Banner>();
        banners=query_360.findBner();
        model.addAttribute("listCover",listCover);
        model.addAttribute("banners",banners);
        model.addAttribute("pageUrl","1");
        model.addAttribute("baseurl","${pageContext.request.contextPath}/videoJson2.do?image=");
        return "index";
    }




    /**
     * 查找HOT
     * @return
     */
    @ResponseBody
    @RequestMapping("/hotJson")
    public List<VideoCover> hotVideo(){

        List<VideoCover> listCover=new ArrayList<VideoCover>();
       listCover=query_360.findHot1();
        return listCover;
    }

    @RequestMapping("/hotJson2")
    public List<VideoCover> hotVideo2(){
        List<VideoCover> listCover=new ArrayList<VideoCover>();
        listCover=query_360.findHot1();
        return listCover;
    }


    /**
     * 根据URL查找Video
     * @param image
     * @return
     */
    @ResponseBody
    @RequestMapping("/videoJson")
    public DS360_dt videoJson(String image){
        DS360_dt dt=query_360.findAllByUrl(image);
        String str="";
        String arr[]=dt.getPurl().split(",");
        for(int i=0;i<arr.length;i++){
            if(i<arr.length-2){
                str+=arr[i]+",";
            }
           else if(i==arr.length-2){
               if(arr[i+1].contains("#")){
                   str+=arr[i];
               }
               else{
                   str+=arr[i]+","+arr[i+1];
               }
            }
        }
        if(str.equals("")){
            return dt;
        }
        dt.setPurl(str);
        return dt;
    }

    @RequestMapping("/videoJson2")
    public String videoJson2(String image,Model model){
        DS360_dt dt=query_360.findAllByUrl(image);
        model.addAttribute("video",dt);
        Map<String,String> map_url=new LinkedHashMap<String, String>();
        String arr[]=dt.getPurl().split(",");
        boolean first_flag=true;
        for(int i=0;i<arr.length;i++){
            if(!arr[i].contains("#")){
                map_url.put("第"+(i+1)+"集",arr[i]);
                if(first_flag){
                    model.addAttribute("first",arr[i]);
                    first_flag=false;
                }
            }
        }
        model.addAttribute("map_url",map_url);
        return "to_player";
    }


    /**
     * 分页查找video封面
     * @param ds
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping("/v_coverPage")
    public List<VideoCover> videoPage(DS360_dt ds,@RequestParam(defaultValue = "1") int page){

        Map<String,Object> map=new HashMap<String, Object>();
        int pageSize=18;
        int totalRecord=query_360.findCoverNum(ds);
        Page<VideoCover> coverPage = new Page<VideoCover>(pageSize, page, totalRecord, null);
        int start = (coverPage.getCurrentPage() - 1) * pageSize;
        map.put("start", start);
        map.put("size", pageSize);
        map.put("ds",ds);
        List<VideoCover> cover=query_360.findVideoPage(map);
        return cover;
    }

    @RequestMapping("/ds")
    public String videoPage2(Model model,DS360_dt ds,@RequestParam(defaultValue = "1") int page){

        Map<String,Object> map=new HashMap<String, Object>();
        int pageSize=24;
        int totalRecord=query_360.findCoverNum(ds);
        Page<VideoCover> coverPage = new Page<VideoCover>(pageSize, page, totalRecord, null);
        int start = (coverPage.getCurrentPage() - 1) * pageSize;
        map.put("start", start);
        map.put("size", pageSize);
        map.put("ds",ds);
        List<VideoCover> cover=query_360.findVideoPage(map);

        List<Banner> banners=new ArrayList<Banner>();
        banners=query_360.findBner();
        model.addAttribute("listCover",cover);
        model.addAttribute("banners",banners);
        model.addAttribute("totalRecord",totalRecord);
        model.addAttribute("pageUrl","ds.do?category="+ds.getCategory()+"&page=");
        model.addAttribute("curr",page);
        model.addAttribute("baseurl","${pageContext.request.contextPath}/videoJson2.do?image=");

        return "index";
    }


    /**
     * 根据标题查找Video
     * @param title
     * @return
     */
    @ResponseBody
    @RequestMapping("/findVideo")
    public List<VideoCover> findVideoByTitle(String title){
        Map<String,String> map =new HashMap<String, String>();
        map.put("title",title);
        List<VideoCover> cover=query_360.findVideoByTitle(map);
        return cover;
    }

    @RequestMapping("/findVideo2")
    public String findVideoByTitle2(String title,Model model){
        Map<String,String> map =new HashMap<String, String>();
        map.put("title",title);
        List<VideoCover> cover=query_360.findVideoByTitle(map);
        model.addAttribute("listCover",cover);
        return "index";
    }


    @ResponseBody
    @RequestMapping("/findBanner")
    public List<Banner> findBanner(){
      List<Banner> banners=new ArrayList<Banner>();
      banners=query_360.findBner();
        return banners;
    }
    @RequestMapping("/findBanner2")
    public List<Banner> findBanner2(){
        List<Banner> banners=new ArrayList<Banner>();
        banners=query_360.findBner();
        return banners;
    }

    @ResponseBody
    @RequestMapping("/findVersion")
    public int findVersion(){
        return 2;
    }



}